<template>
  <div class="container">
    <!-- 头部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>栏目</el-breadcrumb-item>
      <el-breadcrumb-item>栏目列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary" @click="$router.push('/home/cate/add')">添加栏目</el-button>
      <TreeTable
        style="margin-top: 20px"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        index-text="#"
        border
      >
        <template slot="sort" slot-scope="scope">
          <el-input
            min="0"
            type="number"
            :value="scope.row.sort"
            @input="scope.row.sort = $event"
            @blur="editCateOfSort($event, scope.row)"
          />
        </template>
        <template slot="name" slot-scope="scope">{{ scope.row }}</template>
        <!-- 操作 -->
        <template slot="opt" scope="scope">
          <!-- 修改按钮 -->
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            round
            @click="$router.push(`/home/cate/edit/${scope.row.id}`)"
          >
            编辑
          </el-button>
          <!-- 删除按钮 -->
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            round
            @click="removeCateById(scope.row)"
          >
            删除
          </el-button>
        </template>
      </TreeTable>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data() {
    return {
      cateList: [],
      columns: [
        {
          label: '名称',
          prop: 'name'
        },
        {
          label: '排序',
          type: 'template',
          template: 'sort',
          width: '100px'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt',
          width: '200px'
        }
      ]
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    // 获取栏目列表
    async getCateList() {
      const { data: res } = await this.$http.get('/home/cate')
      this.cateList = res
    },
    // 根据id删除栏目
    removeCateById(row) {
      this.$confirm('请确定是否删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`/admin/cate/${row.id}`)
          if (res.status !== 200) return this.$message.error(res.msg)
          this.$message.success(res.msg)
          // 重新获取数据
          this.getCateList()
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    // 修改栏目排序
    async editCateOfSort(e, row) {
      let sort = e.target.value
      // 判断是否修改
      if (row.sort !== sort) return
      if (sort === '') sort = 0
      const { data: res } = await this.$http.put(`/admin/cate/sort/${row.id}`, { sort })
      if (res.status !== 200) return this.$message.error(res.msg)
      this.$message.success(res.msg)
      // 重新获取数据
      this.getCateList()
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .cell {
  display: flex;
  align-items: center;
}
</style>
